package com.example.administrator.robot_shanlong_app;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.robot_shanlong_app.constant.Flag;
import com.example.administrator.robot_shanlong_app.dataconvert.ConvertData;
import com.example.administrator.robot_shanlong_app.dataconvert.DeviceUtils;
import com.example.administrator.robot_shanlong_app.socket.SocketChart;

public class MainActivity extends Activity {
    private static String TAG = MainActivity.class.getSimpleName();
    //指令尾巴
    private String cmd = "0000FFFF";
    //指令头
    private String datah = "BD01";
   //总指令
    private StringBuffer data;
    private byte  [] databyte=new byte[9];
    private SeekBar seekbar_test;
    //文本内容
    private TextView txt_test, txt_val;
    //获取文本内容
    private EditText edit_addr, edit_port;
    private CheckBox client, server;
    //Socket 网络数据
    private SocketChart socketChart;
    private EditText edit_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        InitView();
        Listener();
    }

    //初始化视图
    public void InitView() {
        seekbar_test = findViewById(R.id.seek_test);
        txt_val = findViewById(R.id.val_test);
        txt_test = findViewById(R.id.txt_test);
        edit_addr = findViewById(R.id.ip_addr);
        edit_port = findViewById(R.id.port);
        client = findViewById(R.id.client);
        server = findViewById(R.id.serversocket);
        edit_id = findViewById(R.id.motor_id);

    }

    //监听
    public void Listener() {
        seekbar_test.setOnSeekBarChangeListener(onseekbarchangelistner);
        client.setOnClickListener(onClickListener);
    }

    //数据
    public void InitData() {

    }

    //处理滑条改变事件
    SeekBar.OnSeekBarChangeListener onseekbarchangelistner = new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            txt_test.setText(Integer.toHexString(progress));
            txt_val.setText(String.valueOf(progress));


            if (socketChart != null) {
                if (edit_id.getText() != null) {

                    getCmd(progress);
                    if(data!=null) {
                        Log.i("onseekbarchangelistner", "发送数据" + Integer.toHexString(progress));
                        socketChart.SenderMessage(ConvertData.hexStr2Bytes(data.toString()));
                    }

                }
            }


        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    };

    //监听选择服务端还是客户端
    CheckBox.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.client:
                    server.setChecked(false);
                    client.setChecked(true);
                    SocketInit();
                    break;
                case R.id.serversocket:
                    client.setChecked(false);
                    server.setChecked(true);

            }
        }
    };

    public void SocketInit() {

        if (DeviceUtils.IsIP4(edit_addr.getText().toString()) && DeviceUtils.IsPort(Integer.valueOf(edit_port.getText().toString()))) {

            socketChart = new SocketChart(handler);
            socketChart.SetIpAdress(edit_addr.getText().toString());
            socketChart.SetPort(Integer.valueOf(edit_port.getText().toString()));
            socketChart.start();
            Log.i("SocketStart", "开始监听消息");
        } else {
            Toast.makeText(this, "IP地址不和法或端口错误（不能小于1024）", Toast.LENGTH_SHORT).show();
        }
    }

    //处理更新数据
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {

                case Flag.ERROR_STATE:
                    txt_test.append("\n");
                    txt_test.append("数据出错了");
                    break;
                case Flag.IOFO_STATE:
                    break;
                case Flag.WRITE_STATE:
                    txt_test.append("\n发送数据");
                    txt_test.append(ConvertData.Bytes2HexString(msg.obj.toString().getBytes()));
                    break;
                case Flag.READ_STATE:
                    txt_test.append("\n接收数据");
                    txt_test.append(ConvertData.Bytes2HexString(msg.obj.toString().getBytes()));
                    break;

            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (socketChart != null) {
            socketChart.Close();
        }
    }
    // 处理 指令  处理1-15十六制
    public  void  getCmd(int val)
    {
        data=new StringBuffer();
        //头
        data.append(datah);
        //地址
        //处理当地址小于16时
        if(Integer.valueOf(edit_id.getText().toString())<16)
        {
            data.append("0");
        }
        data.append(Integer.toHexString(Integer.valueOf(edit_id.getText().toString())));
        //P值
        data.append(Integer.toHexString(val));
        //命令
        data.append(cmd);

    }


}
